// H5端路由配置
import React from 'react'
import { createBrowserRouter, Navigate, RouteObject } from 'react-router-dom'
import MainLayout from '../layouts/MainLayout'
import LoginPage from '../pages/LoginPage'
import HomePage from '../pages/HomePage'
import MarketPage from '../pages/MarketPage'
import TradePage from '../pages/TradePage'
import ProfilePage from '../pages/ProfilePage'

// 路由保护组件 - 检查用户是否已登录
function ProtectedRoute({ children }: { children: React.ReactNode }) {
  const token = localStorage.getItem('token')

  if (!token) {
    return <Navigate to="/login" replace />
  }

  return children
}

const routes: RouteObject[] = [
  {
    path: '/login',
    element: <LoginPage />,
  },
  {
    path: '/',
    element: (
      <ProtectedRoute>
        <MainLayout />
      </ProtectedRoute>
    ),
    children: [
      {
        index: true,
        element: <Navigate to="/home" replace />,
      },
      {
        path: '/home',
        element: <HomePage />,
      },
      {
        path: '/news',
        element: <MarketPage />,
      },
      {
        path: '/wallet',
        element: <TradePage />,
      },
      {
        path: '/profile',
        element: <ProfilePage />,
      },
    ],
  }
]

export default createBrowserRouter(routes)

